package botcraft.util;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.nio.ByteBuffer;

import javax.imageio.ImageIO;

public final class Texture {
	private final int width;
	private final int height;
	private final ByteBuffer byteBuffer;
	
	public Texture(final URL url) throws IOException {
		final BufferedImage image = ImageIO.read(url.openStream());
		width = image.getWidth();
		height = image.getHeight();
		byteBuffer = ByteBuffer.allocate(width * height * 3);
		for (int i = 0; i < height; i++) {
			for (int j = 0; j < width; j++) {
				final int argb = image.getRGB(j, i);
				byteBuffer.put((byte) ((argb >>> 16) & 0xff)).put((byte) ((argb >>> 8) & 0xff)).put((byte) (argb & 0xff));
			}
		}
		byteBuffer.rewind();
	}
	
	public int getWidth() {
		return width;
	}
	
	public int getHeight() {
		return height;
	}
	
	public ByteBuffer getByteBuffer() {
		return byteBuffer;
	}
}
